/*
 * 创建日期 2005-10-5
 *
 * 更改所生成文件模板为
 * 窗口 > 首选项 > Java > 代码生成 > 代码和注释
 */
package com.vanda.report.view;

import java.util.List;

import org.apache.commons.validator.GenericValidator;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRow;

import com.vanda.report.ExcelUtils;
import com.vanda.report.ReportException;
import com.vanda.report.ReportInnerData;
import com.vanda.report.element.BeanElement;

/**
 * 将数据写入Cell标签 <br>
 * <p>
 * 
 * @author eagle <br>
 * @version Version 1.00 <br>
 */
public class CellElement extends BeanElement {
	/** 指定行数偏移量 */
	private int row_offset;
	/** 指定列数偏移量 */
	private int col_offset;
	private int col_next = 1;
	/** 指定Cell值 */
	private String message;
	/** 指定Cell的格式 */
	private String format;
	
	private boolean bRowOffset = false;
	private boolean bColOffset = false;

	public void validator() throws ReportException {
		//Validation.checkRequired("CellElement","col_offset",col_offset,-1);
	}

	/**
	 * 标签执行方法，递归执行
	 * @param obj
	 */
	public void execute(Object obj) throws ReportException {
		ReportInnerData reportData = (ReportInnerData) obj;
		//String property = super.getProperty();

		try {

			//取得值对象
			Object objBean = super.getNameObject(reportData);

			//format行数，对应formatSheet
			int formatRowNum = reportData.getFormatRowNum();
			//开始列数
			int startCol = reportData.getStart_col();
			if (startCol <= 0) {
				startCol = 1;
			}
			//开始行数
			int startRow = reportData.getStart_row();
			//如果没有设置startRow或没有设置row_offset，则开始行数为currentRow
			if (startRow <= 0 || bRowOffset == false) {
				startRow = reportData.getCurrentRow() + 1;
			}
			int rowOffset = row_offset;
			//if (rowOffset == INIT_OFFSET) {
			//	rowOffset = 0;
			//}

			int colNum = 0;
			
			if(bColOffset == true){
				colNum = startCol + col_offset - 1;
			}else{
				colNum = reportData.getCurrentCol() + col_next - 1;
				reportData.setCurrentCol(colNum+1);
			}
			
			HSSFCell cell =
				ExcelUtils.getCell(
					reportData.getReportSheet(),
					startRow + rowOffset - 1,
					colNum);

			HSSFRow formatRow = null;
			if (formatRowNum > 0) {
				formatRow =
					reportData.getFormatSheet().getRow(formatRowNum - 1);
			}

			//取得Cell值
			Object cellValue = getCellValue(reportData,objBean);

			setCell(cell, cellValue, formatRow, colNum, formatRowNum);

			super.execute(reportData);
		} catch (ReportException e) {
			throw e;
		} catch (Exception e) {
			e.printStackTrace();
			throw new ReportException(e);
		}

	}
	
	protected Object getCellValue(ReportInnerData reportData,Object rowBean) throws Exception{
		String property = super.getProperty();
		Object cellValue = null;
		
		if (!GenericValidator.isBlankOrNull(property)) {
			try {
				Object cellObj = super.getProperty(rowBean, property);
				if(cellObj instanceof List){
					StringBuffer buffer = new StringBuffer();
					for(int i=0;i<((List)cellObj).size();i++){
						buffer.append(((List)cellObj).get(i)).append("\n");
					}
					cellValue = buffer.toString();
				}else{
					cellValue = cellObj;
				}
			} catch (Exception e) {
				throw new ReportException(e);
			}

		} else {
			cellValue = message;
		}
		
		return cellValue;
	}
	


	private void setCell(
		HSSFCell cell,
		Object cellValue,
		HSSFRow formatRow,
		int colNum,
		int formatRowNum)
		throws ReportException {
		if (cellValue == null) {
			cellValue = "";
		}

		//设置单元格的样式
		/*if (formatRow != null) {
			HSSFCell formatCell = formatRow.getCell((short) colNum);
			if (formatCell == null) {
				throw new ReportException(
					"Format Row["
						+ formatRowNum
						+ "]:Col["
						+ (colNum + 1)
						+ "] is not exist!");
			}
			cell.setCellStyle(formatCell.getCellStyle());
			cell.setCellType(formatCell.getCellType());
		}*/

		ExcelUtils.setCellValue(cell,cellValue,format);
		
	}

	/**
	 * @return
	 */
	public int getCol_offset() {
		return col_offset;
	}

	/**
	 * @return
	 */
	public int getRow_offset() {
		return row_offset;
	}

	/**
	 * @param i
	 */
	public void setCol_offset(int i) {
		bColOffset = true;
		col_offset = i;
	}

	/**
	 * @param i
	 */
	public void setRow_offset(int i) {
		bRowOffset = true;
		row_offset = i;
	}

	/**
	 * @return
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param string
	 */
	public void setMessage(String string) {
		message = string;
	}

	/**
	 * @return
	 */
	public int getCol_next() {
		return col_next;
	}

	/**
	 * @param i
	 */
	public void setCol_next(int i) {
		col_next = i;
	}

	/**
	 * @return
	 */
	public String getFormat() {
		return format;
	}

	/**
	 * @param string
	 */
	public void setFormat(String string) {
		format = string;
	}

}
